package com.ruoyi.web.controller.common;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.AliHttpUtils;
import com.ruoyi.system.domain.WeatherData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Api(value = "天气信息接口", tags = "天气信息接口")
@RestController
@RequestMapping("/weather")
public class WeatherController {

    @Autowired
    RedisCache redisCache;


    @PostMapping("query")
    @ApiOperation("查询天气接口")
    public AjaxResult getWeathers() {
        Map<String,Object> map = new HashMap<>();

        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        String t1 = DateUtils.parseDateToStr(DateUtils.YYYYMMDD, now);
        String str = redisCache.getCacheObject(CacheConstants.WEATHER_KEY + t1);
        if (StringUtils.isBlank(str)){
            str = getOneDayWeather(t1);
            redisCache.setCacheObject(CacheConstants.WEATHER_KEY + t1, str);
        }
        WeatherData weatherData = JSON.to(WeatherData.class, str);
        map.put( "today" ,weatherData);
        for (int i = 0; i < 5; i++) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            Date time = calendar.getTime();
            String timeStr = DateUtils.parseDateToStr(DateUtils.YYYYMMDD, time);
            String timeData = redisCache.getCacheObject(CacheConstants.WEATHER_KEY + timeStr);
            if (StringUtils.isBlank(timeData)){
                timeData = getOneDayWeather(timeStr);
                redisCache.setCacheObject(CacheConstants.WEATHER_KEY + timeStr, timeData);
            }
            WeatherData to = JSON.to(WeatherData.class, timeData);
            map.put(timeStr, to);
        }
        return AjaxResult.success(map);
    }

    /**
     * 获取一天的天气情况
     * @param date
     * @return
     */
    private String getOneDayWeather(String date) {
        String host = "https://jmweather.market.alicloudapi.com";
        String path = "/weather/query/by-date";
        String method = "POST";
        String appcode = "ad456deba64d4e43bf889fba8c1227df";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("areaId", "101100408");
        bodys.put("date", date);
        try {
            HttpResponse response = AliHttpUtils.doPost(host, path, method, headers, querys, bodys);
            String weatherStr = EntityUtils.toString(response.getEntity());
            return weatherStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


}
